package com.platform.modules.quality.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import com.platform.modules.sys.entity.SysOrgEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.modules.prj.entity.PrjProjectEntity;
import com.platform.modules.quality.dto.QlyAcceptanceListByAppDto;
import com.platform.modules.quality.dto.QlyInspectionListByAppDto;
import com.platform.modules.quality.dto.QlyInspectionStatusQuantityByAppDto;
import com.platform.modules.quality.entity.QlyAcceptanceEntity;
import com.platform.modules.quality.entity.QlyAcceptancePersonnelEntity;
import com.platform.modules.quality.entity.QlyInspectionEntity;
import com.platform.modules.quality.service.QlyAcceptancePersonnelService;
import com.platform.modules.quality.service.QlyAcceptanceService;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.quality.vo.QlyAcceptanceStatusQuantityByAppVo;
import com.platform.modules.quality.vo.QlyInspectionStatusQuantityByAppVo;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.util.DocUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Controller
 *
 * @author biaohuaming
 * @date 2020-02-24 15:28:20
 */
@Api(tags = "QlyAcceptanceController|质量验收")
@RestController
@RequestMapping("qly/acceptance")
public class QlyAcceptanceController extends AbstractController {

    @Autowired
    private QlyAcceptanceService qlyAcceptanceService;
    @Autowired
    private DocUtil docUtil;
    @Autowired
    private QlyAcceptancePersonnelService qlyAcceptancePersonnelService;

    
    /**
     * @descr 查询 质量验收记录，只查询自己创建的质量验收记录，不考虑其他数据范围<br>
     * @param currPage
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询 质量验收记录PC")
    @PostMapping("/list")
    public RestResponse query(Integer currPage, Integer limit, @RequestParam(required = false) String prjName, 
    		@RequestParam(required = false) String cityNum, @RequestParam(required = false) String countyNum, 
    		@RequestParam(required = false) String sgCorpName, @RequestParam(required = false) String buildCorpName,
    		@RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate,
    		@RequestParam(required = false) String[] accType) {
    	Map<String, Object> params = new HashMap<>();
//        原来 ....
//        params.put("accUserId", getUserId());
//        params.put("page", PageUtil.getCurrPage(currPage));
//        params.put("limit", PageUtil.getLimit(limit));
//        params.put("cityNum", cityNum);
//        params.put("countyNum", countyNum);
//        params.put("prjName", prjName);
//        params.put("sgCorpName", sgCorpName);
//        params.put("buildCorpName", buildCorpName);
//        params.put("startDate", startDate);
//        params.put("endDate", endDate);
//        params.put("accType", accType);



    	// AND GAN
        params.put("page", PageUtil.getCurrPage(currPage));
        params.put("limit", PageUtil.getLimit(limit));
        params.put("prjName", prjName);
        params.put("sgCorpName", sgCorpName);
        params.put("buildCorpName", buildCorpName);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("accType", accType);


        SysOrgEntity orgEntity = getOrg();
        // 当前是项目组织
        if(orgEntity == null){
            // params.put("buildCorpId", getOrgNo());
            params.put("prjId", getOrgNo());
        }else{ // 实体组织
            // 如果是监管机构
            if("5".equals(orgEntity.getOrgType())){
                if(orgEntity.getOrgLevel()==2){
                    params.put("provinceNum", orgEntity.getProvinceNo());
                    params.put("countyNum", countyNum);
                    params.put("cityNum", cityNum);
                }else if(orgEntity.getOrgLevel()==3){
                    params.put("cityNum", orgEntity.getCityNo());
                    params.put("countyNum", countyNum);
                }else if(orgEntity.getOrgLevel()==4){
                    params.put("countyNum", orgEntity.getCountyNo());
                }
            }else if ("1".equals(orgEntity.getOrgType())){
                params.put("buildCorpId", getOrgNo());
            }else if ("9".equals(orgEntity.getOrgType())){
                params.put("sgCorpId", getOrgNo());
            }
        }
        // AND GAN


    	Page<QlyAcceptanceEntity> page = qlyAcceptanceService.queryPage(params);
        return RestResponse.success().put("page", page);
    } 
    
    
    
    /**
     * @descr 查询 质量验收记录，只查询自己创建的质量验收记录，不考虑其他数据范围<br>
     * @param currPage
     * @param limit
     * @return
     */
//    @ApiOperation(value = "查询 质量验收记录PC")
//    @PostMapping("/list2")
//    @Deprecated
//    public CommonResult<List<QlyAcceptanceEntity>> list2(@RequestParam(required = false) String attId) {
//    	Map<String, Object> params = new HashMap<>();
//    	params.put("attId", attId);
//    	List<QlyAcceptanceEntity> list = qlyAcceptanceService.queryList(params);
//        return CommonResult.success(list);
//    } 
    
    @ApiOperation(value = "图纸分析 PC/APP")
    @PostMapping("/drawing/analysis")
    public CommonResult<List<QlyAcceptanceEntity>> drawingAnalysis(
    		@RequestParam(required = true) String attId) {
    	Map<String, Object> params = new HashMap<>();
    	params.put("attId", attId);
    	List<QlyAcceptanceEntity> list = qlyAcceptanceService.queryList(params);
        return CommonResult.success(list);
    } 
    
    
    
    @ApiOperation(value = "查询 质量验收记录PC")
    @PostMapping("/page")
    @Deprecated
    public RestResponse page(@RequestParam(required = false) String attId) {
    	Map<String, Object> params = getParams();
    	params.put("attId", attId);
    	IPage<QlyAcceptanceEntity> page = qlyAcceptanceService.queryPage(params);
    	return RestResponse.success().put("page", page);
    } 
    
    
    
    /**
     * APP查询 质量验收记录
     * @param queryDto
     * @return
     */
    @ApiOperation(value = "APP查询 质量验收记录")
    @PostMapping("/listByApp")
    public CommonResult<Page<QlyAcceptanceEntity>> listByApp(@Valid @RequestBody QlyAcceptanceListByAppDto queryDto) {
    	Map<String, Object> params = new HashMap<>();
        params.put("page", PageUtil.getCurrPage(queryDto.getCurrPage()));
        params.put("limit", PageUtil.getLimit(queryDto.getLimit()));
        params.put("prjId", queryDto.getPrjId());
        params.put("accType", queryDto.getAccType());
    	Page<QlyAcceptanceEntity> page = qlyAcceptanceService.queryPageByApp(params);
    	return CommonResult.success(page);
    } 
    
    /**
     * 保存质量验收记录
     * @param qlyInspectionEntity
     * @return
     */
    @ApiOperation(value = "保存质量验收记录")
    @PostMapping("save")
    public CommonResult<QlyAcceptanceEntity> save(@RequestBody QlyAcceptanceEntity qlyAcceptanceEntity) {
    	qlyAcceptanceService.saveAll(qlyAcceptanceEntity);
        return CommonResult.success(qlyAcceptanceEntity);
    }
    
    /**
     * 根据ID查询质量验收记录详情
     *
     * @param qlyAcceptance
     * @return RestResponse
     */
    @ApiOperation(value = "根据ID查询质量验收记录详情", notes = "根据ID查询质量验收记录详情")
    @PostMapping("/info")
    public CommonResult<QlyAcceptanceEntity> info(@RequestBody QlyAcceptanceEntity qlyAcceptance) {
    	QlyAcceptanceEntity qlyAcceptanceEntity = qlyAcceptanceService.getById(qlyAcceptance.getAccId());
    	//组装"验收人列表"
    	if(null==qlyAcceptanceEntity) {
    		return CommonResult.error("未找到质量验收记录");
    	}else {
    		List<QlyAcceptancePersonnelEntity> personnels = qlyAcceptancePersonnelService.queryByAccId(qlyAcceptance.getAccId());
    		qlyAcceptanceEntity.setPersonnels(personnels);
    	}
        return CommonResult.success(qlyAcceptanceEntity);
    }
    
    @ApiOperation(value = "上传 - 质量验收图片")
    @PostMapping("/acce/pic")
    public RestResponse saveAcceptancePic(MultipartFile file, String accId) {
    	docUtil.createAtt(file, "", accId);
        return RestResponse.success();
    }
    
    
    @ApiOperation(value = "查询 - 质量验收图片列表")
    @GetMapping("/acce/pic")
    public RestResponse getAcceptancePic(String accId) {
        return RestResponse.success().put("list", docUtil.getAttListByAttBusinessId(accId));
    }
    
    
    @ApiOperation(value = "删除 - 质量验收图片")
    @DeleteMapping("/acce/pic")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "delete", name = "attIds", value = "文件id(支持多个,逗号分割)", dataType = "String")
    })
    public RestResponse deleteAcceptancePic(String attIds) {
    	if(StringUtils.isNotBlank(attIds)) {
    		docUtil.deleteAtts(Arrays.asList(attIds.split(",")));
    	}
        return RestResponse.success();
    }

    /**
     * @descr 质量验收统计(状态数量)APP
     * @return
     */
    @ApiOperation(value = "质量验收统计(状态数量)APP")
    @PostMapping("/statusQuantity")
    public CommonResult<QlyAcceptanceStatusQuantityByAppVo> statusQuantity(@Valid @RequestBody QlyInspectionStatusQuantityByAppDto statusQuantityDto) {
    	QlyAcceptanceStatusQuantityByAppVo statusQuantity = qlyAcceptanceService.statusQuantity(statusQuantityDto.getPrjId());
        return CommonResult.success(statusQuantity);
    } 
}
